﻿using Common;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SFJDManagement
{
    /// <summary>
    /// Page_DataErr.xaml 的交互逻辑
    /// </summary>
    public partial class Page_DataErr : Page
    {
        List<ClassBrigade> listBri = Runtime.xmlBrigade.GetAll();
        private List<ClassData> listErr = null;
        private List<ClassData> listCache = null;
        private StackPanel layout = null;

        public Page_DataErr()
        {
            InitializeComponent();
            Ini();
        }

        private void Ini()
        {
            layout = new StackPanel()
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Top,
                CanVerticallyScroll = true,
            };
            listErr = Runtime.xmlTemp.GetDataErr();
            listCache = Runtime.xmlTemp.GetDataCache();
            ShowErrTable();
            ShowCacheTable();
            ScrollViewer layroot = new ScrollViewer()
            {
                HorizontalScrollBarVisibility = ScrollBarVisibility.Auto,
                VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
            };
            layroot.Content = layout;
            Content = layroot;
        }

        private void ShowErrTable()
        {
            if(listErr != null)
            {
                StackPanel grid_err = new StackPanel();
                Label label_Err = new Label()
                {
                    Content = "发生错误的数据：",
                    FontSize = 20,
                    Foreground = Brushes.Red,
                    BorderBrush = Brushes.Red,
                    BorderThickness = new Thickness(0, 0, 0, 2),
                };
                grid_err.Children.Add(label_Err);
                foreach(ClassData data in listErr)
                {
                    grid_err.Children.Add(GetBox(data));
                }
                Label label_Err2 = new Label()
                {
                    Content = "请联系管理员",
                    FontSize = 16,
                    Padding = new Thickness(0, 10, 0, 10),
                    Margin = new Thickness(0, 10, 0, 80),
                };
                grid_err.Children.Add(label_Err2);
                layout.Children.Add(grid_err);
                //Button button_Err = new Button()
                //{
                //    Content = "将发生错误的数据重新登记",
                //    FontSize = 16,
                //    Padding = new Thickness(0, 10, 0, 10),
                //    Margin = new Thickness(0, 10, 0, 80),
                //};
                //button_Err.Click += Button_Err_Click;
                //grid_err.Children.Add(button_Err);
                //layout.Children.Add(grid_err);
            }
        }

        private void Button_Err_Click(object sender, RoutedEventArgs e)
        {
            Runtime.xmlTemp.RemoveDataErr(listErr[0]);
            Runtime.xmlTemp.AddDataCache(listErr[0]);
            Win_Main2.instance.Layout_Function.Content = new Page_Register(listErr[0]);
        }

        private void ShowCacheTable()
        {
            if (listCache != null)
            {
                StackPanel grid_Cache = new StackPanel();
                Label label_Cache = new Label()
                {
                    Content = "登记失败的数据：",
                    FontSize = 20,
                    Foreground = Brushes.Green,
                    BorderBrush = Brushes.Green,
                    BorderThickness = new Thickness(0, 0, 0, 2),
                };
                grid_Cache.Children.Add(label_Cache);
                foreach (ClassData data in listCache)
                {
                    grid_Cache.Children.Add(GetBox(data));
                }
                Button button_Cache = new Button()
                {
                    Content = "重新登记号码",
                    FontSize = 16,
                    Padding = new Thickness(0, 10, 0, 10),
                    Margin = new Thickness(0, 10, 0, 0),
                };
                button_Cache.Click += Button_Cache_Click;
                grid_Cache.Children.Add(button_Cache);
                layout.Children.Add(grid_Cache);
            }
        }

        private void Button_Cache_Click(object sender, RoutedEventArgs e)
        {
            //xmlTemp.RemoveDataCache(listCache[0]);
            Win_Main2.instance.Layout_Function.Content = new Page_Register(listCache[0]);
        }

        private TextBlock GetBox(ClassData data)
        {
            TextBlock b = new TextBlock()
            {
                Text = DataTranslate(data),
                FontSize = 14,
                Margin = new Thickness(0, 10, 0, 10),
            };
            return b;
        }

        private string DataTranslate(ClassData data)
        {
            string b = string.Empty;
            b += data.ID + ", ";
            b += data.Code + ", ";

            foreach (ClassBrigade brigade in listBri)
            {
                if (brigade.Guid == data.Brigade)
                {
                    if (brigade.Brigade[brigade.Brigade.Count() - 1] != '队')
                    {
                        b += brigade.Brigade + ", ";
                    }
                    else
                    {
                        b += brigade.City + brigade.Brigade + ", ";
                    }
                    break;
                }
            }
            b += data.Contact + ", ";
            b += "事故时间：" + data.TimeHappen.ToString("MM-dd") + ", ";
            b += "划号时间：" + data.TimeArrange.ToString("MM-dd") + ", 车辆：";
            foreach(ClassVehicle vehicle in data.Vehicles)
            {
                b += vehicle.Number + vehicle.Type + "、 ";
            }
            b = b.Substring(0, b.Length - 2);
            return b;
        }
    }
}
